<?php
class PersistenceException extends Exception {
	
}


class FilePersistence {
	
	private $dataPath;
	
	public function FilePersistence() {
		$this->dataPath = 'data/';
		if (file_exists($this->dataPath . 'WinterProduct.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'WinterProduct.txt');
		}
		if (file_exists($this->dataPath . 'BagsProduct.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'BagsProduct.txt');
		}
		if (file_exists($this->dataPath . 'CWearProduct.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'CWearProduct.txt');
		}
		if (file_exists($this->dataPath . 'CampingProduct.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'CampingProduct.txt');
		}
		if (file_exists($this->dataPath . 'OtherProduct.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'OtherProduct.txt');
		}
		if (file_exists($this->dataPath . 'Settlement.txt')) {
			//do nothing
		} else {
			//creating new file
			$this->createFile($this->dataPath . 'Settlement.txt');
		}
		
	}
	
	private function createFile($fileName) {
		$fileHandler = fopen($fileName, 'wt') or die('cannot open file');
		fclose($fileHandler);
	}
	
	private function getCSVFile() {
		
	}
	
	public function getAboutUsInfo() {
		$aboutUsFilePath = $this->dataPath . 'AboutUs.txt';
		//$fileHandler = fopen($aboutUsFilePath, 'rt') or die ('cannot open file');
		$result = file_get_contents($aboutUsFilePath);
		//fclose($fileHandler);
		return $result;
	}
	
	public function getHeadOffice() {
		$headOffice = $this->dataPath . 'HeadOffice.txt';
		//$fileHandler = fopen($aboutUsFilePath, 'rt') or die ('cannot open file');
		$result = file_get_contents($headOffice);
		//fclose($fileHandler);
		return $result;
	}
	
	public function getShowroom() {
		$headOffice = $this->dataPath . 'Showroom.txt';
		//$fileHandler = fopen($aboutUsFilePath, 'rt') or die ('cannot open file');
		$result = file_get_contents($headOffice);
		//fclose($fileHandler);
		return $result;
	}
	
	/*
	public function getAboutUsInfo() {
		$result = '';
		$dbhandle = sqlite_open('data/winningDB.dat', 0666, $error);
		if (!$dbhandle) die ($error);
		
		$query = " SELECT DESC FROM ABOUTUS ";
		$dbResult = sqlite_query($dbhandle, $query);
		if (!$dbResult) die("Cannot execute query.");
		
		$row = sqlite_fetch_array($dbResult, SQLITE_NUM);
		$result = $row[0];
		sqlite_close($dbhandle);
		
		return $result;
	}
	*/
	
	public function authenticate($username, $password) {
		$result = '';
		$dbhandle = sqlite_open('data/winningDB.dat', 0666, $error);
		if (!$dbhandle) die ($error);
		
		$query = " SELECT count(*) FROM users WHERE username = '" . $username . "'" .
				 " AND password = '" . $password . "'";
		$dbResult = sqlite_query($dbhandle, $query);
		if (!$dbResult) die("Cannot execute query.");
		
		$row = sqlite_fetch_array($dbResult, SQLITE_NUM);
		if ($row[0] == '1') {
			$result = true;
		} else {
			$result = false;
		}
		
		sqlite_close($dbhandle);
		
		return $result;
	}
	
	
	public function getSystemProperty($propertyKey) {
		$configArr = parse_ini_file($this->dataPath . 'SystemConfig.ini');
		return $configArr[$propertyKey];
		/*
		$fileHandler = fopen($systemFile, 'rt') or die ('cannot open file');
		$key='';
		$value='';
		while ($lineStr = fscanf($fileHandler, '%[a-zA-Z0-9,. ]=%[a-zA-Z0-9,. ]')) {
			list($key, $value) = $lineStr;
			if ($propertyKey == $key) {
				return $value;
			}
		}
		fclose($fileHandler);
		*/
	}
	
	//public function get
	/*
	public function getDataById($dataType, $id) {
		if ($dataType == 'WinterProduct') {
			if (file_exists($)) {
				
			}

		} else if ($dataType == 'BagsProduct') {
		
		} else if ($dataType == 'CWearProduct') {
		
		} else if ($dataType == 'CampingProduct') {
		
		} else if ($dataType == 'OtherProduct') {
			
		} else if ($dataType == 'Settlement') {
		
		} else {
			throw new PersistenceException('Illegal dataType found');
		}
	}
	*/
	
	public function saveOrUpdateData($dataType, $object) {
		try {
			if ($dataType == 'WinterProduct') {
				//initialize the array
				if (get_class($object) != 'WinterProduct') {
					throw new PersistenceException('Invalid datatype to save object');
				} 
				$winterProductArr = array();
				for ($i=0; $i < 3; $i++) {
					array_push($winterProductArr, 0);
				}
				$winterProductArr[0] = $object->getProductID();
				$winterProductArr[1] = $object->getProductName();
				$winterProductArr[2] = $object->getProductDesc();
			}
			if ($dataType == 'AboutUs') {
				
			}
		} catch (Exception $e) {
			throw new PersistenceException($e->getTraceAsString());
		}
	}
	
	/*
	public function saveAboutUsInfo($aboutUsInfo) {
		$dbhandle = sqlite_open('data/winningDB.dat', 0666, $error);
		if (!$dbhandle) die ($error);
		
		$query = " UPDATE ABOUTUS set DESC = '" . $aboutUsInfo . "'";
		$status = sqlite_exec($dbhandle, $query);
		if (!$status) die("Cannot execute statement.");
		
		sqlite_close($dbhandle);
	}
	*/

	public function saveAboutUsInfo($aboutUsInfo) {
		$aboutUsPath = $this->dataPath . 'AboutUs.txt';
		$aboutUsInfo = trim($aboutUsInfo);
		//remove the newline and carriage
		$aboutUsInfo = preg_replace('/\r\n/','',$aboutUsInfo);		
		$fp = fopen($aboutUsPath, "w+t");
		if (flock($fp, LOCK_EX)) { // do an exclusive lock
		    fwrite($fp, $aboutUsInfo);
		    flock($fp, LOCK_UN); // release the lock
		} else {
		    echo "Couldn't lock the file !";
		}
		fclose($fp);
	}
	
	public function saveHeadOffice($headOfficeInfo) {
		$headOfficePath = $this->dataPath . 'HeadOffice.txt';
		$headOfficeInfo = trim($headOfficeInfo);
		//remove the newline and carriage
		$headOfficeInfo = preg_replace('/\r\n/','',$headOfficeInfo);		
		$fp = fopen($headOfficePath, "w+t");
		if (flock($fp, LOCK_EX)) { // do an exclusive lock
		    fwrite($fp, $headOfficeInfo);
		    flock($fp, LOCK_UN); // release the lock
		} else {
		    echo "Couldn't lock the file !";
		}
		fclose($fp);
	}

	public function saveShowroom($showroomInfo) {
		$showroomPath = $this->dataPath . 'Showroom.txt';
		$showroomInfo = trim($showroomInfo);
		//remove the newline and carriage
		$showroomInfo = preg_replace('/\r\n/','',$showroomInfo);		
		$fp = fopen($showroomPath, "w+t");
		if (flock($fp, LOCK_EX)) { // do an exclusive lock
		    fwrite($fp, $showroomInfo);
		    flock($fp, LOCK_UN); // release the lock
		} else {
		    echo "Couldn't lock the file !";
		}
		fclose($fp);
	}
	
	private function appendCSVItem() {
		
	}
	
}

?>